package second;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class UserProxy implements InvocationHandler{

	private Object target;
	
	public UserProxy(Object target) {
		super();
		this.target = target;
	}

	public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
		
		if(method.getName().equals("save")){
			
			System.out.println("处理之前");
			method.invoke(target, args);
			System.out.println("处理之后");
		}
		return null;
	}
	
	public static Object generateProxy(UserProxy proxy, User user){
		
		return Proxy.newProxyInstance(user.getClass().getClassLoader(), 
				user.getClass().getInterfaces(), proxy);
	}
	
	public static void main(String args[]){
		
		User user = new User();
		UserMapper userProxy = (UserMapper)UserProxy.generateProxy(new UserProxy(user), user);
		
		userProxy.save("涂有", 23);
	}
}
